<template>
  <div>
    <el-form class="query-form" @submit.native.prevent :inline="true">
      <el-form-item>
        <select-address
          v-model="params.address_code"
          auto-top
          clearable
          placeholder="所属地"
          @change="getData()"
        ></select-address>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.customer_name"
          :clearable="true"
          placeholder="主体名称查询"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.lender_idcard"
          :clearable="true"
          placeholder="身份证号码"
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          :clearable="true"
          placeholder="经营者姓名"
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <c-select
          placeholder="档案卡状态"
          clearable
          v-model="params.project_status"
          @change="getData()"
          :options="{ 0: '未建档', 1: '已建档' }"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()" type="primary">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      class="table-sm"
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    >
      <template slot="submit_person_name" slot-scope="scope">
        <div class="text-truncate" :title="scope.row.submit_person_name">
          {{ scope.row.submit_person_name }}
        </div>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      :before-close="handleCreditClose"
      top="30px"
      width="80%"
    >
      <el-tabs>
        <el-tab-pane label="数字档案">
          <!-- <credit-file :view-mode="reportViewMode"
                       ref="credit"
                       @success="regetData"
                       :query-id="queryId"></credit-file> -->
          <credit-file
            :view-mode="reportViewMode"
            @success="regetData"
            :query-id="queryId"
          ></credit-file>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <attach-view :queryId="queryId" :visible="reportVisible"></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <!-- <el-dialog :visible.sync="visible"
               width="80%">
      <gov-credit :queryId="relatedId"></gov-credit>
    </el-dialog> -->
    <attach-file :customerRelatedId="queryId" :visible.sync="attachVisible"></attach-file>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SelectAddress from '@/components/select/SelectAddress'
import CreditFile from '@/gov/components/creditFile/GovCredit'
import AttachFile from '@/credit/views/city/program/Attach'
import AttachView from '@/credit/components/AttachView'
import { enumISNom } from '@/gov/plugins/enum'
export default {
  components: {
    SelectAddress,
    CreditFile,
    AttachFile,
    AttachView,
    // govCredit
  },
  props: {},
  data() {
    return {
      visible: false,
      relatedId: '',
      tableName: 'customer_related',
      tableId: '',
      enumISNom,
      params: {
        index: 1,
        size: 15,
        customer_name: '',
        lender_idcard: '',
        address_code: '',
      },
      loading: false,

      tableData: [],
      tableCount: 0,
      tableColumns: [
        { attrs: { label: '所属地区', prop: 'address', minWidth: '160px' } },
        {
          attrs: { label: '主体名称', prop: 'customer_name', minWidth: '160px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '经营者姓名', prop: 'corp_name' } },
        { attrs: { label: '身份证号码', prop: 'lender_idcard' } },
        { attrs: { label: '建档时间', prop: 'archiveTime', minWidth: '120px' } },
        { attrs: { label: '操作用户', prop: 'submit_person_name' }, slot: 'submit_person_name' },
        { attrs: { label: '推荐时间', prop: 'recommend_time', minWidth: '120px' } },
        {
          attrs: { label: '档案卡是否完成', prop: 'project_status' },
          process: v => enumISNom[v] || '否',
        },
        {
          attrs: { label: '操作', align: 'center', minWidth: '160px' },
          operations: [
            {
              content: '修改',
              icon: 'fa fa-edit',
              event: this.handleEdit,
              ifRender: row => row.editEnable,
            },
            // { content: '归集', icon: 'fa fa-hourglass', event: this.handleFile, ifRender: row => row.editEnable },
            { content: '附件', icon: 'fa fa-clipboard', event: this.handleAttach },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
            // { content: '查看', name: '查看', event: this.handleNew } // 新档案卡测试
            // { content: '删除', icon: 'fa fa-trash', attrs: { 'class': 'text-danger py-0 px-2' }, event: this.handleDelete }
          ],
        },
      ],

      // 信用档案
      reportVisible: false,
      reportViewMode: false,
      queryId: '',
      submit_type: '',
      // 附件编辑
      attachVisible: false,
    }
  },
  computed: {
    ...mapGetters(['org', 'user']),
    isUserTown() {
      return +this.user.orgRegionLever
    },
  },
  watch: {},
  created() {
    this.params.address_code = this.$utils.getAddressCode()
    this.submit_type = this.$utils.getClientOrgClass() === 'org_gov_ny' ? '1' : '4'
    this.getData()
  },
  mounted() {},
  methods: {
    handleNew(row) {
      this.relatedId = row.customer_related_id
      this.visible = true
    },
    regetData() {
      this.reportVisible = false
      this.getData()
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      this.params['submit_type.eq'] = this.submit_type
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, this.params, {
          N1create_time: 'desc',
          N2recommend_time: 'desc',
        })
      )
      this.loading = false

      const data = (res && res.data) || {}
      this.tableData = (data.data || []).map(item => {
        // 项目调查 0 未调查 1 已调查 2 调查中 3 已通过
        // item.editEnable = [1, 3, 4].indexOf(+item.project_status) < 0
        // 2020.4.16 市政府和区县政府不能填写信用档案卡
        item.archiveTime = this.$utils.dateFormat(item.archive_complete_time, 'yyyy-MM-dd') || '-'
        item.editEnable = +item.is_approval !== 1 && this.isUserTown === 4 // 2020.3.6 政府端可以修改未初审的项目
        // item.creditFileEditEnable = +item.project_status === 0 || +item.project_status === 2
        return item
      })
      this.tableCount = data.totalCount || 0
    },
    handleEdit(row) {
      this.queryId = row.customer_related_id
      this.reportVisible = true
    },
    // handleDelete(row) {
    //   this.$utils.msgbox({
    //     message: `确定删除：${row.customer_name}?`,
    //     type: 'warning',
    //     beforeClose: () => this.$api.deleteData(this.$serviceName, this.tableName, row.id)
    //   }).then(() => {
    //     this.$message.success('删除成功！')
    //     this.getData()
    //   }).catch(() => this.$message.error(`删除失败，请重试！`))
    // },
    handleFile(row) {
      this.$utils
        .msgbox({
          message: `确定归集：${row.customer_name}?`,
          type: 'info',
          beforeClose: () =>
            this.$api.updateData('credit', 'customer_related', row.customer_related_id, {
              last_archive_time: this.$utils.dateFormat(new Date()),
            }),
        })
        .then(() => {
          this.$message.success('归集成功！')
          if (this.$refs.credit) {
            this.$refs.credit.resetData()
            this.$refs.credit.init()
          }
          this.getData()
        })
        .catch(() => this.$message.error(`归集失败，请重试！`))
    },
    handleView(row) {
      this.queryId = row.customer_related_id
      this.reportViewMode = true
      this.reportVisible = true
    },
    handleCreditClose(done) {
      setTimeout(() => {
        this.reportViewMode = false
        this.getData(this.params.index)
      }, 250)
      done()
    },
    handleAttach(row) {
      this.queryId = row.customer_related_id
      this.attachVisible = true
    },
  },
}
</script>

<style lang="scss" scoped></style>
